<?php


namespace app\admin\model;


use app\admin\validate\Market;
use app\common\Constants\ErrorCode;
use think\Model;
use think\response\Json;
use think\facade\Db;

class MarketModel extends Common
{

    const FIELD = ['id', 'name', 'kingdee_code'];
    /**
     * @var string
     */
    protected $name = 'shop_market';

    protected $defaultSoftDelete = 0;

    protected $autoWriteTimestamp = true;

    /**
     * 驼峰格式转换
     * @var bool
     */
    protected $convertNameToCamel = true;

    //protected $append =['count'];

    /**
     * @return int
     * @author ran
     * @date 2021-11-17 10:24
     * mailbox 466180170@qq.com
     */
   /* public function getCountAttr()
    {
        return $this->shop()->count();
    }*/
    /**
     * @return \think\model\relation\HasMany
     * @author ran
     * @date 2021-11-17 10:24
     * mailbox 466180170@qq.com
     */
    public function shop()
    {
        return $this->hasMany(ShopModel::class, 'market_id', 'id')->field('shop_id,market_id,shop_name');
    }
    /**
     * 创建店铺市场资源
     * @author ran
     * @date 2021-09-22 16:37
     * mailbox 466180170@qq.com
     */
    public function createShopAdminMarketResource(array $request)
    {
        $validate = new Market();
        if (!$validate->scene('create')->check($this->humpToUnderscore($request))) {
            $this->error = $validate->getError();
            return false;
        }
        try {
            $res = $this->create($request);
            if ($res->id) {
                return true;
            }
            $this->error = '店铺市场资源入库失败';
            return false;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            return false;
        }
    }
    /**
     * 更新店铺市场资源
     * @author ran
     * @date 2021-09-22 16:37
     * mailbox 466180170@qq.com
     */
    public function updateShopAdminMarketResource(array $request)
    {
        $validate = new Market();
        if (!$validate->scene('update')->check($request)) {
            $this->error = $validate->getError();
            return false;
        }
        try {
            $this->update($request, ['id' => $request['id']]);
            return true;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            return false;
        }
    }
    /**
     * 获取店铺市场列表
     * @author ran
     * @date 2021-09-22 16:37
     * mailbox 466180170@qq.com
     */
    public function getShopAdminMarketListResource(int $page, int $limit = 10)
    {
        $connect = $this->field(self::FIELD);
        return [
            'list' => $connect->page($page, $limit)->select(),
            'dataCount' => $this->count()
        ];
    }

    /**
     * 获取店铺市场列表
     * @author ran
     * @date 2021-09-22 16:37
     * mailbox 466180170@qq.com
     */
    public function getShopAdminMarketWithListResource(int $page, int $limit = 10)
    {
        $connect = $this->field(self::FIELD);
        $list = $connect->page($page,$limit)->select();
        $shop =Db::name('shop')->field('shop_id as shopId,shop_name as shopName,market_id')->select()->toArray();
        $shopBind=[];
        foreach ($shop as $v) {
            $shopBind[$v['market_id']][] = $v;
        }
        foreach ($list as $value){
            $value['shop']=$shopBind[$value['id']]??[];
        }
        return [
            'list' => $list,
            'dataCount' => $connect->count()
        ];
       /* $connect = $this->field(self::FIELD);
        $list = $connect->page($page, $limit)->select();
        return [
            'list' => $list,
            'dataCount' => $connect->count()
        ];*/
    }

    /**
     * 获取 员工信息
     * @param int $id
     * @author ran
     * @date 2021-10-18 9:02
     * mailbox 466180170@qq.com
     */
    public function getShopAdminMarketInfoResource(int $id)
    {
        $field = ['id', 'name','kingdee_code'];
        return $this->field($field)->find($id);
    }
	/**
	 * 获取市场信息
	 */
	public function getMarketList(array $shopIds,$request)
	{
	    $where = [];
	
	    if($shopIds){
	
	        $marketId = Db::name('shop')->where('shop_id','in',$shopIds)->column('market_id');
	
	        array_push($where,['id','in',$marketId]);
	
	    }
	
	    $marketName = $request['marketName'] ?? '';
	    if($marketName){
	        array_push($where,['name','like',$marketName.'%']);
	    }
	
	    $list = Db::name('shop_market')->where($where)->column('id,name');
	    
	    return $list;
	}



}